package com.uqbar.arkanoid.components;

import java.awt.Color;
import java.awt.Graphics2D;

import com.uqbar.arkanoid.scenes.ArkanoidScene;
import com.uqbar.vainilla.DeltaState;
import com.uqbar.vainilla.GameComponent;
import com.uqbar.vainilla.appearances.Circle;
import com.uqbar.vainilla.colissions.CollisionDetector;

public class Pelota extends GameComponent<ArkanoidScene> {
	
	private boolean start = false;
	private int radius =10;
	int speed = 700;
	double i;
	double j;
	private Raqueta raqueta;
	static double anguloMayor = Math.PI/3;
	static double anguloMenor = -Math.PI/3;
	
	public Pelota(Color baseColor,double x,double y,Raqueta raqueta){
		this.setAppearance(new Circle(baseColor, 2*this.radius));
		this.setX(x);
		this.setY(y);
		this.raqueta = raqueta;
	}
	
	public boolean isStart(){
		return start;
	}
	
	public void start() {
		this.start = true;
	}

	public void stop() {
		this.start = false;
	}

	public void onSceneActivated(){
		
		this.i = 5 * 2 - 1;
		this.j = -5 * 2 - 1;
		
		double m = Math.sqrt(this.i * this.i + this.j * this.j);
		
		this.i = this.i / m;
		this.j = this.j / m;
		
		super.onSceneActivated();
	}
	
	@Override
	public void render(Graphics2D graphics) {
		super.render(graphics);
	}
	
	public void update(DeltaState deltaState) {
		double advanced = this.speed * deltaState.getDelta();

	    if(this.getX() <= 0 )
	    	this.i = this.i * -1;
		
	    
	    if((this.getX() + radius *2) >= (this.getGame().getDisplayWidth()))
	    	this.i=this.i* -1;

	    
		if(this.getY() <= 0)
			this.j = this.j * -1;
		
		
		if(colisionaConRaqueta(this.raqueta))
			this.definirAnguloSalida();	
		
		if(getX() >= this.getGame().getDisplayWidth())
			this.setX(this.getGame().getDisplayWidth() - 1);
		
		if(getX() <= 0)
			this.setX(1);
		
		if(getY() <= 0)
			this.setY(1);
		
		
		if((this.getY() + radius*2) >= (this.getGame().getDisplayHeight())){
			this.getScene().getMarcador().decreaseLive();
			this.restart();
			this.stop();
			if(this.getScene().getMarcador().getLives() < 0){
				this.getScene().fin();
			}
		}
		
		if(this.start){
		  this.move(this.i * advanced, this.j * advanced);
		  this.coordinarImagen();
		}else{
			this.setX(getScene().getRaqueta().getX()+40);
			this.setY(getScene().getRaqueta().getY()-20);
			this.coordinarImagen();
		}
		super.update(deltaState);
	}
	
	private void coordinarImagen(){
		  this.getScene().getSkinPelota().setX(this.getX());
		  this.getScene().getSkinPelota().setY(this.getY());
	}
	
	private void restart() {
		this.setX(this.getScene().getRaqueta().getX()+40);
		this.setY(this.getScene().getRaqueta().getY()-20);	
		this.coordinarImagen();
	}

	private void definirAnguloSalida() {
		double puntoDeColision = obtenerPuntoDeColision();
		
		double anguloNuevo = ((anguloMayor - anguloMenor) / raqueta
				.getAppearance().getWidth())
				* puntoDeColision
				+ anguloMenor;
		
		this.i = Math.sin(anguloNuevo);
		this.j = Math.cos(anguloNuevo) *-1;
		
		
		
		if(this.getY() > this.getGame().getDisplayHeight() - 20)
		this.setY(this.getGame().getDisplayHeight() - 20);
		
		
		
	}

	private double obtenerPuntoDeColision() {
		if (this.getX() > raqueta.getX()&& this.getX() + this.getAppearance().getWidth() < raqueta.getX() + raqueta.getAppearance().getWidth()) {
			double xCentroPelota = this.getX() + this.getAppearance().getWidth() / 2;
			return xCentroPelota - raqueta.getX();
		} else if (this.getX() < raqueta.getX()) {
			return 0;
		} else {
			return raqueta.getAppearance().getWidth();
		}
	}

	private boolean colisionaConRaqueta(Raqueta raqueta) {
		return CollisionDetector.INSTANCE.collidesCircleAgainstRect(
				this.getX(), this.getY(), 
				this.getAppearance().getWidth() / 2, raqueta.getX(),
				raqueta.getY(), raqueta.getAppearance().getWidth(),
				raqueta.getAppearance().getHeight());
	}

}
